using System;
using Randomize.Core.Exceptions;

namespace Randomize.Core.Generators
{
    public class DoubleGenerator : RangeGenerator<double>, IDoubleGenerator
    {
        private const double DefaultMin = 0;

        private const double DefaultMax = 1;

        public int? FractionalPartSize { get; set; }

        public DoubleGenerator(Random random) : base(random)
        {
        }

        protected override double GenerateRandomValue()
        {
            double min = this.Min ?? DefaultMin;
            double max = this.Max ?? DefaultMax;

            if (max < min)
            {
                throw new GenerationConfigurationException("Configured min value cannot be more than max");
            }

            double generated = this.RandomInstance.NextDouble();

            double randomValue = generated*(max - min) + min;

            if (this.FractionalPartSize.HasValue)
            {
                return Math.Round(randomValue, this.FractionalPartSize.Value);
            }

            return randomValue;
        }
    }
}